from datetime import datetime
from typing import List
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_stock.service.monthly_service import MonthlyService
from module_stock.entity.vo.monthly_vo import DeleteMonthlyModel, MonthlyModel, MonthlyPageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


monthlyController = APIRouter(prefix='/stock/monthly', dependencies=[Depends(LoginService.get_current_user)])


@monthlyController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('stock:monthly:list'))]
)
async def get_stock_monthly_list(
    request: Request,
monthly_page_query: MonthlyPageQueryModel = Depends(MonthlyPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    monthly_page_query_result = await MonthlyService.get_monthly_list_services(query_db, monthly_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=monthly_page_query_result)

@monthlyController.get(
    '/all',
    response_model=List[MonthlyModel],
    dependencies=[Depends(CheckUserInterfaceAuth('stock:monthly:all'))],
)
async def get_all_stock_monthly(request: Request, symbol: str, query_db: AsyncSession = Depends(get_db)):
    # 获取全量数据
    monthly_list_result = await MonthlyService.get_all_monthly_services(query_db, symbol)
    logger.info('获取成功')

    return ResponseUtil.success(data=monthly_list_result)


@monthlyController.post('', dependencies=[Depends(CheckUserInterfaceAuth('stock:monthly:add'))])
@ValidateFields(validate_model='add_monthly')
@Log(title='股票月线', business_type=BusinessType.INSERT)
async def add_stock_monthly(
    request: Request,
    add_monthly: MonthlyModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_monthly.create_time = datetime.now()
    add_monthly.update_time = datetime.now()
    add_monthly_result = await MonthlyService.add_monthly_services(query_db, add_monthly)
    logger.info(add_monthly_result.message)

    return ResponseUtil.success(msg=add_monthly_result.message)


@monthlyController.put('', dependencies=[Depends(CheckUserInterfaceAuth('stock:monthly:edit'))])
@ValidateFields(validate_model='edit_monthly')
@Log(title='股票月线', business_type=BusinessType.UPDATE)
async def edit_stock_monthly(
    request: Request,
    edit_monthly: MonthlyModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_monthly.update_by = current_user.user.user_name
    edit_monthly.update_time = datetime.now()
    edit_monthly_result = await MonthlyService.edit_monthly_services(query_db, edit_monthly)
    logger.info(edit_monthly_result.message)

    return ResponseUtil.success(msg=edit_monthly_result.message)


@monthlyController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('stock:monthly:remove'))])
@Log(title='股票月线', business_type=BusinessType.DELETE)
async def delete_stock_monthly(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_monthly = DeleteMonthlyModel(ids=ids)
    delete_monthly_result = await MonthlyService.delete_monthly_services(query_db, delete_monthly)
    logger.info(delete_monthly_result.message)

    return ResponseUtil.success(msg=delete_monthly_result.message)


@monthlyController.get(
    '/{id}', response_model=MonthlyModel, dependencies=[Depends(CheckUserInterfaceAuth('stock:monthly:query'))]
)
async def query_detail_stock_monthly(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    monthly_detail_result = await MonthlyService.monthly_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=monthly_detail_result)


@monthlyController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('stock:monthly:export'))])
@Log(title='股票月线', business_type=BusinessType.EXPORT)
async def export_stock_monthly_list(
    request: Request,
    monthly_page_query: MonthlyPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    monthly_query_result = await MonthlyService.get_monthly_list_services(query_db, monthly_page_query, is_page=False)
    monthly_export_result = await MonthlyService.export_monthly_list_services(monthly_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(monthly_export_result))
